use serde_json::{json, Value};
use crate::feature::Base;
use crate::group::send_message_group;
use crate::model::gaode_map::WeatherInfo;
use crate::model::WebSocketMessage;
use crate::util::request::get;

pub struct Comfort {}

impl Base for Comfort {
    async fn send_message(content: Option<String>, web_socket_message: WebSocketMessage) {
        if let Some(_content) = content {
            let data = match get_comfort().await {
                Ok(live) => live,
                Err(err) => {
                    send_message_group(Some(err), web_socket_message).await;
                    return;
                }
            };
            let reply = format!(
                "\n{}", data
            );
            send_message_group(Some(reply), web_socket_message).await;
        }
    }
}

async fn get_comfort() -> Result<String, String> {
    let response = get("https://v.api.aa1.cn/api/api-wenan-anwei/index.php", json!({
        "type": "json"
    })).await?;
    let value = response.json::<Value>().await.unwrap();
    Ok(value["anwei"].to_string())
}